package vn.smartoffice.promail.controller;

import java.util.HashMap;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.val;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import vn.smartoffice.auth.UserService;
import vn.smartoffice.promail.entity.MailAccount;
import vn.smartoffice.promail.services.MailAccountService;

@VariableResolver(DelegatingVariableResolver.class)
public class MailAccountCtr {

	public static final Integer MAX_ACCOUNT_NUMBER = 3;

	@WireVariable private UserService userService;
	@WireVariable private MailAccountService mailAccountService;
	@Getter @Setter private ListModelList<MailAccount> accountListModel;
	@Getter @Setter private MailAccount selectedAccount;
	@Getter private boolean canAddNewAccount = false;

	@Init
	public void init() {
		List<MailAccount> listAccounts = mailAccountService.findByUserId(userService.getCurrentUserId());
		canAddNewAccount = listAccounts.size() == MAX_ACCOUNT_NUMBER;
		accountListModel = new ListModelList<MailAccount>(listAccounts);
		selectedAccount = null;
	}

	@Command("add")
	public void add() {
		selectedAccount = new MailAccount();
		selectedAccount.setUser(userService.getCurrentUser());
		val dataArgs = new HashMap<String, Object>();
		dataArgs.put("maiAccount", selectedAccount);
		val window = (Window) Executions.createComponents("dialog/account.zul", null, dataArgs);
		window.doModal();
	}

	@Command("edit")
	public void edit() {
		val dataArgs = new HashMap<String, Object>();
		dataArgs.put("maiAccount", selectedAccount);
		val window = (Window) Executions.createComponents("dialog/account.zul", null, dataArgs);
		window.doModal();
	}

	@Command("delete")
	public void delete() {
		if (selectedAccount.isReadOnly()) {
			Messagebox.show(Labels.getLabel("delete.errorMessages"), Labels.getLabel("wraning.title"), Messagebox.OK,
					Messagebox.EXCLAMATION);
		} else {
			Messagebox.show(Labels.getLabel("delete.messages"), Labels.getLabel("delete.title"), Messagebox.OK
					| Messagebox.CANCEL, Messagebox.QUESTION, new EventListener<Event>() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getName().equals("onOK")) {
						accountListModel.remove(selectedAccount);
						mailAccountService.deleteById(selectedAccount.getId());
						init();
						BindUtils.postNotifyChange(null, null, MailAccountCtr.this, "selectedAccount");
						BindUtils.postNotifyChange(null, null, MailAccountCtr.this, "accountListModel");
						BindUtils.postNotifyChange(null, null, MailAccountCtr.this, "canAddNewAccount");
					}
				}
			});
		}
	}

	@GlobalCommand
	@NotifyChange({ "selectedAccount", "accountListModel", "canAddNewAccount" })
	public void refreshMailAccount(@BindingParam("accountId") MailAccount mailAccount) {
		init();
		selectedAccount = mailAccount;
	}

}
